package com.railway.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.railway.bean.LoseCreditUser;
import com.railway.service.LoseCreditService;
import com.railway.service.impl.LoseCreditServiceImpl;

@WebServlet("/checkLoseCreditUser.do")
public class CheckLoseCreditUserServlet  extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		String name = req.getParameter("user_name");
		String id = req.getParameter("user_id_num");
		if(name == null) {
			name = "";
		}else {
			name = req.getParameter("user_name");
		}
		if(id == null) {
			id = "";
		}else {
			id = req.getParameter("user_id_num");
		}
		LoseCreditService lcs = new LoseCreditServiceImpl();
		List<LoseCreditUser> loserCredit = lcs.selectUser(name, id);
		/*req.setAttribute("loseCredit",lcs.selectUser(name, id));
		System.out.println(loserCredit);
		req.getRequestDispatcher("creditInformation.jsp").forward(req, resp);*/
		
		 ObjectMapper om = new ObjectMapper(); resp.setCharacterEncoding("UTF-8");
		 //设置 后端给前端传回的编码格式为utf-8 
		 resp.setContentType("application/json");// 后端告知前端 返回的数据 是 json格式(不是html /css)
		 resp.getWriter().print(om.writeValueAsString(loserCredit));
		 
	}
	
}
